package com.yanqu.road.dao.impl.wingroom;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.UserWingRoomData;
import com.yanqu.road.entity.wingroom.WingRoomRegion;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zfr
 * @date ：2022/2/17 14:55
 */
public class WingRoomDataDaoImpl extends TempDataObjectDao<UserWingRoomData> {
    public static String insertSql = "INSERT INTO t_u_wingroom_data(userId,lv,regionMap,comfortValue,getEventRewardTimes, " +
            "timesResetTime,bgm) values(?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UserWingRoomData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRegionMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getComfortValue()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGetEventRewardTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTimesResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBgm()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wingroom_data set lv=?,regionMap=?,comfortValue=?,getEventRewardTimes=?," +
            "timesResetTime=?,bgm=? where userId=?";
    @Override
    protected boolean updateData(UserWingRoomData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLv()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getRegionMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getComfortValue()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGetEventRewardTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTimesResetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getBgm()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWingRoomData parseData(ResultSet rs) throws SQLException {
        UserWingRoomData data = new UserWingRoomData();
        data.setUserId(rs.getLong("userId"));
        data.setLv(rs.getInt("lv"));
        data.setRegionMap(JSONObject.parseObject(rs.getString("regionMap"), new TypeReference<Map<Integer, WingRoomRegion>>() {
        }));
        data.setComfortValue(rs.getLong("comfortValue"));
        data.setGetEventRewardTimes(rs.getInt("getEventRewardTimes"));
        data.setTimesResetTime(rs.getLong("timesResetTime"));
        data.setBgm(rs.getString("bgm"));
        return data;
    }

    public UserWingRoomData getUserWingRoomData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        UserWingRoomData data = null;
        String sql = "select * from t_u_wingroom_data where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return data;
    }
}
